﻿using System;
using System.Collections.Generic;
using System.Text;
using RollBackLib.src.core;
using log4net;
using log4net.Config;
using System.Reflection;
using ReliableSession.src;
using ReliableSession;

namespace RollbackConsole
{
    public class RollbackConsole
    {
        BackupJob job = null;
        private static readonly ILog log = LogManager.GetLogger(typeof(RollbackConsole));

        public RollbackConsole(string jobName)
        {
            job = new BackupJob();
            job.initialize("C:\\Rollback\\Backup", jobName);
            job.addPath("C:\\Intel");
        }

        public void startJob()
        {
            RollbackLib.src.utils.Mailer.sendUsingGmail("rollback.support@gmail.com", "r0llback", "kumar.vijay@gmail.com","Backup Finished", "This is the body of the message");
            BackupRestoreRunner runner = new BackupRestoreRunner(statusUpdateHandler);
            BackupInfo binfo = new BackupInfo(BackupInfo.BackupType.BACKUP, job, statusUpdateHandler);
            job.m_OverwriteNewerFiles = true;
            job.m_extraLoggingEnabled = false;
            runner.startAsync(binfo);
            runner.waitForJobToFinish();
            log.Info("Finished backup job " + job.m_Id.m_Name);
            runner.stopAllThreads();
        }

        public void statusUpdateHandler(BackupInfo bInfo)
        {
            //log.Info(bInfo.m_Statistics.m_operationMsg);
        }

        public static void Main(string[] args)
        {
            string exeParentDir = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string[] paths = {
                                 "./Resources/", 
                                 "../../Resources/",
                                 exeParentDir + "/../Resources",
                                 exeParentDir + "/../../Resources",
                             };
            RollbackLib.src.utils.Logger.configureLogger(paths);

            bool runReliableSession = true;
            FifoBuffer fb = new FifoBuffer(64 * 1024);
            byte[] data = Encoding.ASCII.GetBytes("Hello world");
            fb.write(data, 10, 0, data.Length);
            fb.write(data, 5, 0, data.Length);

            {
                // test out of order chunk list.
                Transport testChunkList = new Transport();
                testChunkList.testAddToIndex();
            }

            if (runReliableSession)
            {
                TestSession ts = new TestSession();
            }
            else
            {
                RollbackConsole console = new RollbackConsole("TestJob");
                console.startJob();
            }

            System.Console.ReadKey();
        }
    }
}
